package p.s;

import java.net.URI;
import java.net.URLDecoder;

public class Query 
    extends java.util.HashMap<String,String>
{

    public Query(URI path){
        super();
        this.add(path.getQuery());
    }


    public void add(String qs){
        if (null != qs){
            String[] pairs = qs.split("[\\?\\&]");
            for (String pair : pairs){
                String[] set = pair.split("=");
                if (1 == set.length)
                    this.put(URLDecoder.decode(set[0]),"");
                else if (2 <= set.length){
                    this.put(URLDecoder.decode(set[0]),URLDecoder.decode(set[1]));
                }
            }
        }
    }

    public static void main(String[] argv){
        if (1 == argv.length){
            try {
                URI uri = new URI(argv[0]);

                Query q = new Query(uri);

                for (String name : q.keySet()){
                    String value = q.get(name);
                    System.out.println(name+" := "+value);
                }
                System.exit(0);
            }
            catch (Exception exc){
                exc.printStackTrace();
                System.exit(1);
            }
        }
        else {
            System.out.println("Usage: <uri>");
            System.exit(1);
        }
    }
}
